home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_352 / treewalk / magic.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  129 lines

  1. /*
  2.  * magic - code for dealing with magic strings. All functions take a
  3.  *    null terminated string, and  return either the code to be used
  4.  *    in place of the string, or -1 indicating that things failed.
  5.  *    If -1 is a valid value, then we got problems...
  6.  *
  7.  *    Copyright (C) 1989  Mike Meyer
  8.  *
  9.  *    This program is free software; you can redistribute it and/or modify
  10.  *    it under the terms of the GNU General Public License as published by
  11.  *    the Free Software Foundation; either version 1, or any later version.
  12.  *
  13.  *    This program is distributed in the hope that it will be useful,
  14.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *    GNU General Public License for more details.
  17.  *
  18.  *    You should have received a copy of the GNU General Public License
  19.  *    along with this program; if not, write to the Free Software
  20.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. #include <exec/types.h>
  24. #include <libraries/dos.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <math.h>
  29. #include <dos.h>
  30. #include "errors.h"
  31.  
  32. static short monthdays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30} ;
  33.  
  34. long
  35. date(char *in) {
  36.     static int    today = 0, now, weekday ;    /* Do now only once */
  37.     short        month, dom, year, hour ;    /* Scratch */
  38.     long        outday, outminute ;
  39.     char        day[FMSIZE], *time ;
  40.     
  41.  
  42.     if (!today) {
  43.         getclk((unsigned char *) &day) ;
  44.         weekday = day[0] ;
  45.         now = day[4] * 60 + day[5] ;
  46.         year = 2 + day[1] ;    /* Convert to datestamp base of '78 */
  47.         month = day[2] ;
  48.         today = day[3] - 1 ;    /* Days count from 0, not 1 */
  49.         if ((year + 2) % 4 == 0 && month > 2) today += 1 ;
  50.         while (--month)
  51.             today += monthdays[month - 1] ;
  52.         today += year * 365 + (year + 1) / 4 ;
  53.         }
  54.     outday = today ;
  55.  
  56.     time = stpblk(stptok(stpblk(in), day, FMSIZE, " \t")) ;
  57.  
  58.     if (isdigit(*day)) {    /* Hard date, use it that way */
  59.         if (strchr(day, ':')) /* No, it's not a date, it's a time */
  60.             time = day ;
  61.         else if (sscanf(day, "%d/%d/%d", &month, &dom, &year) != 3)
  62.                 return -1 ;    /* Bleah */
  63.         else if (year > 99) return -1 ;
  64.         else {
  65.             year -= 78 ;
  66.             outday = dom - 1 + year * 365 + (year + 1) / 4 ;
  67.             if ((year + 2) % 4 == 0 && month > 2) outday += 1 ;
  68.             while (--month)
  69.                 outday += monthdays[month - 1] ;
  70.             }
  71.         }
  72.     else if (!stricmp(day, "noon")) time = day ;
  73.     else if (!stricmp(day, "yesterday")) outday -= 1 ;
  74.     else if (stricmp(day, "today")) {    /* Today means no change */
  75.         /* These are ... the days of the week */
  76.         if (!stricmp(day, "sunday")) dom = 0 ;
  77.         else if (!stricmp(day, "monday")) dom = 1 ;
  78.         else if (!stricmp(day, "tuesday")) dom = 2 ;
  79.         else if (!stricmp(day, "wednesday")) dom = 3 ;
  80.         else if (!stricmp(day, "thursday")) dom = 4 ;
  81.         else if (!stricmp(day, "friday")) dom = 5 ;
  82.         else if (!stricmp(day, "saturday")) dom = 6 ;
  83.         else return -1 ;
  84.  
  85.         outday -= (dom == weekday) ? 7 : abs(dom - weekday) ;
  86.         }
  87.  
  88.     if (!*time) return outday * 24 * 60 ;    /* Well, we dont do time */
  89.     if (!stricmp(time, "noon")) outminute = 12 * 60 ;
  90.     else if (!isdigit(*time)
  91.          || sscanf(time, "%d:%d", &hour, &outminute) != 2) return -1 ;
  92.     else outminute += hour * 60 ;
  93.  
  94.     if (outminute >= now && outday == today) outday -= 1 ;
  95.  
  96.     return outday * 24 * 60 + outminute ;
  97.     }
  98.  
  99. /* HIDDEN isn't real, but we want to have a way to specify it */
  100. #define FIBB_HIDDEN    7
  101. #define FIBF_HIDDEN    (1<<FIBB_HIDDEN)
  102.  
  103. long
  104. prot(char *in) {
  105.     long    out = 0 ;
  106.  
  107.     while (*in)
  108.         switch (*in++) {
  109.             case 'd':    out |= FIBF_DELETE ;
  110.             break ;
  111.             case 'e':    out |= FIBF_EXECUTE ;
  112.             break ;
  113.             case 'w':    out |= FIBF_WRITE ;
  114.             break ;
  115.             case 'r':    out |= FIBF_READ ;
  116.             break ;
  117.             case 'a':    out |= FIBF_ARCHIVE ;
  118.             break ;
  119.             case 'p':    out |= FIBF_PURE ;
  120.             break ;
  121.             case 's':    out |= FIBF_SCRIPT ;
  122.             break ;
  123.             case 'h':    out |= FIBF_HIDDEN ;
  124.             break ;
  125.             default: return -1 ;
  126.             }
  127.     return out ;
  128.     }
  129.